package com.arm.controller.api.product;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.module.ProductEntity;
import com.arm.service.ProductEntityService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Api(description = "产品实体接口")
@RequestMapping("/api/productEntity")
@RestController
public class APIProductEntityController extends BaseController {

    @Autowired
    ProductEntityService productEntityService;


    @ApiOperation(value = "获取产品实体列表", notes = "获取产品实体列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品编码", required = true, paramType = "path", dataType = "long"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int")
    })
    @GetMapping("/list/{productId}")
    public ResultObj<PageInfo<ProductEntity>> getProductEntityList(@PathVariable("productId")Long productId, Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();
        try {
            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }

            PageHelper.startPage(page, pageSize);
            List<ProductEntity> productEntityList = productEntityService.getProductEntityList(new HashMap<String, String>() {{
                put("productId", String.valueOf(productId));

            }});
            PageInfo<ProductEntity> pageinfo = new PageInfo<>(productEntityList);
            resultObj.setData(pageinfo);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }

    @ApiOperation(value = "获取产品实体", notes = "获取产品实体")
   /* @ApiImplicitParams({
            @ApiImplicitParam(name = "productEntityId", value = "产品实体Id", required = true, paramType = "path", dataType = "Long")
    })*/
    @GetMapping("/{productEntityId}")
    public ResultObj<ProductEntity> getProductEntityByProductEntityId(@PathVariable("productEntityId") Long productEntityId) {

        ResultObj resultObj = new ResultObj();
        try {
            ProductEntity productEntity = productEntityService.getProductEntityByProductEntityId(productEntityId);

            resultObj.setData(productEntity);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;

    }
    @ApiOperation(value = "编辑产品实体", notes = "编辑产品实体")
    @PostMapping(value = "/modifiyProductEntity")
    public ResultObj modifiyProductEntity(@RequestBody ProductEntity productEntity, @RequestParam(value = "productEntityPicUrl", required = false) MultipartFile productEntityPicUrl, @RequestParam(value = "productEntityInfoPicUrl", required = false) MultipartFile productEntityInfoPicUrl) {
        ResultObj resultObj = new ResultObj();
        String pic = "";
        String filePath = "autumnrainmorning2/product";
        logger.info("=====================>>>" + filePath);
        try {
            if (productEntity.getProductId() == null) {
                throw new MyRunTimeExcption("产品不能为空！");
            }

            if (productEntityPicUrl != null) {
                try {

                    pic = upLoadFile(productEntityPicUrl.getInputStream(), filePath);
                    logger.info("resourceUrl=============>>>" + pic);
                    productEntity.setProductEntityPic(pic);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (productEntityInfoPicUrl != null) {
                try {

                    pic = upLoadFile(productEntityInfoPicUrl.getInputStream(), filePath);
                    logger.info("resourceUrl=============>>>" + pic);
                    productEntity.setPicUrl(pic);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            productEntity = productEntityService.modifyProductEntity(productEntity);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj;
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
}
